package com.ermace.robo.jumper;

import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.ermace.robo.jumper.game.Sound;
import com.ermace.robo.jumper.game.GameObjects.Bitmaps;

public class LoadingScreen extends Activity {
	
	private static double SCREEN_WIDTH;
	private static double SCREEN_HEIGHT;
	
	public static double getScreenWidth() {
		return SCREEN_WIDTH;
	}
	
	public static double getScreenHeight() {
		return SCREEN_HEIGHT;
	}
	
	private int numberOfBitmaps;
	private int bitmapsloaded = 0;

	private LoadingContent loading;

	private int pos = 0;
	private ImageView img;
	private Intent MainIntent;
	
	private LoadingView loadingView;
	
	public void incrementBitMapsLoaded() {
		bitmapsloaded++;
		loading.update();
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        setContentView(R.layout.loadingscreen);
		
//        img = (ImageView) findViewById(R.id.loadingImg);
//        img.startAnimation(AnimationUtils.loadAnimation(this, R.anim.loading_rot));

		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		SCREEN_HEIGHT = metrics.heightPixels;
		SCREEN_WIDTH = metrics.widthPixels;
		
		
	    MainIntent = new Intent(this, MainActivity.class);
	    
        loadingView = (LoadingView) findViewById(R.id.loadingScreenView);
	    
	    loading = new LoadingContent();
		loading.execute();
		
		
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    	return loadingView.handleEvent(event);
    }

	private class LoadingContent extends AsyncTask<URL, Integer, Long> {

    	private long previousUpdate =  0;
    	
    	public void update() {
    		int progress = numberOfBitmaps / bitmapsloaded;
    		publishProgress(progress);
    	}
    	
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}
		
	    @Override
		protected Long doInBackground(URL... urls) {
	    	LoadingScreen parent = LoadingScreen.this;
	    	
	    	Bitmaps.loadAllBitmaps(getResources(), parent);
	    	/*MapList.getInstance();*/
	    	Sound.getInstance(getBaseContext());
	    	return null;
	    }

	    @Override
		protected void onProgressUpdate(Integer... progress) {
	    	long currentTime = System.currentTimeMillis();
	    	long timeInterval = currentTime - previousUpdate;
	    	
	    	previousUpdate = currentTime;
	    }

	    @Override
		protected void onPostExecute(Long result) {
	    	startActivity(MainIntent);
	    	finish();

	    }
	 }
    
}
